package com.songcha.module_bookdetail.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.VelocityTracker
import android.view.ViewConfiguration
import android.widget.Button
import android.widget.FrameLayout
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.ToastUtil
import kotlin.math.absoluteValue

/***
 * 书籍详情手势识别，滑动跳转阅读
 */
class BookDetailGestureFrameLayout
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : FrameLayout(context,attrs,defStyle,defRes){
    private var mLastX=0f
    private var mLastY=0f
    private var mListener:IOnBookDetailFlipPageListener?=null
    private var mScaledMinimumFlingVelocity=0
    private var mScaledMaximumFlingVelocity=0
    private var mVelocityTracker:VelocityTracker?=null

    enum class FlipPageDirection{
        LEFT_RIGHT,
        RIGHT_LEFT,
        TOP_BOTTOM,
        BOTTOM_TOP
    }


    interface IOnBookDetailFlipPageListener{
        fun onGestureFlipPage(direction:FlipPageDirection)
        //fun onGestureClick()
    }


    init{
        initView(context,attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        mScaledMinimumFlingVelocity=ViewConfiguration.get(context).scaledMinimumFlingVelocity
        mScaledMaximumFlingVelocity=ViewConfiguration.get(context).scaledMaximumFlingVelocity
    }

    fun setOnBookDetailFlipPageListener(listener:IOnBookDetailFlipPageListener){
        mListener=listener
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        //LogUtil.log("aaa dispatchTouchEvent",ev)

        if(ev==null)  return super.dispatchTouchEvent(ev)

        if(mVelocityTracker==null)
            mVelocityTracker=VelocityTracker.obtain()
        mVelocityTracker!!.addMovement(ev)

        when(ev.action){
            MotionEvent.ACTION_DOWN->{
                mLastX=ev.x
                mLastY=ev.y
            }
            MotionEvent.ACTION_UP->{
                var direction=FlipPageDirection.LEFT_RIGHT
                var isFlipPage=false
                val deltaX=(ev.x-mLastX).absoluteValue
                val deltaY=(ev.y-mLastY).absoluteValue

                if(deltaY>deltaX){
                    if(ev.y>=mLastY){
                        direction=FlipPageDirection.TOP_BOTTOM
                    }else{
                        direction=FlipPageDirection.BOTTOM_TOP
                    }
                }else{
                    if(ev.x>=mLastX){
                        direction=FlipPageDirection.LEFT_RIGHT
                    }else{
                        direction=FlipPageDirection.RIGHT_LEFT
                    }
                }


                if(direction==FlipPageDirection.LEFT_RIGHT || direction==FlipPageDirection.RIGHT_LEFT){
                    //LogUtil.log("aaa1",deltaX,ScreenUtil.getScreenWidth()/4)
                    if(mVelocityTracker!=null){
                        mVelocityTracker!!.computeCurrentVelocity(1000,mScaledMaximumFlingVelocity.toFloat())
                        val velocityX=mVelocityTracker!!.xVelocity
                        //LogUtil.log("velocityX",velocityX,mScaledMinimumFlingVelocity)
                        if(velocityX.absoluteValue>=mScaledMinimumFlingVelocity){
                            isFlipPage=true
                        }
                    }
                    /*if(deltaX>=ScreenUtil.getScreenWidth()/4){
                        isFlipPage=true
                    }*/
                }else{
                    //LogUtil.log("aaa2",deltaY,ScreenUtil.getScreenHeight()/4)
                    if(mVelocityTracker!=null){
                        mVelocityTracker!!.computeCurrentVelocity(1000,mScaledMaximumFlingVelocity.toFloat())
                        val velocityY=mVelocityTracker!!.yVelocity
                        //LogUtil.log("velocityY",velocityY)
                        if(velocityY.absoluteValue>=mScaledMinimumFlingVelocity){
                            isFlipPage=true
                        }
                    }
                    /*if(deltaY>=ScreenUtil.getScreenHeight()/4){
                        isFlipPage=true
                    }*/
                }

                mLastX=0f
                mLastY=0f
                mVelocityTracker?.recycle()
                mVelocityTracker?.clear()
                mVelocityTracker=null

                if(isFlipPage){
                    mListener?.onGestureFlipPage(direction)
                }else{
                    //mListener?.onGestureClick()
                }
            }

        }

        return super.dispatchTouchEvent(ev)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        //LogUtil.log("aaa onInterceptTouchEvent",ev)
       // LogUtil.log("aaa",ev)
        if(ev==null)  return super.onInterceptTouchEvent(ev)

        when(ev.action){
            MotionEvent.ACTION_UP->{
                //LogUtil.log("aaa onInterceptTouchEvent",ev)
            }
        }


        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        //LogUtil.log("aaa onTouchEvent",event)
        return super.onTouchEvent(event)
    }
}